﻿@page "/frogsoft/permission/role/role_list"
@rendermode InteractiveServer
@inject IDialogService DialogService
@inject Frogsoft.Permission.RoleService roleService
@using Frogsoft.Permission.ShareDtos
@using Frogsoft.Permission.Dtos
@using System.Diagnostics
@attribute [Authorize(Policy = "system_permission_role")]

<PageTitle>角色管理</PageTitle>
<h3>角色列表</h3>

<div style="margin-bottom: 16px;">
    <FluentButton Appearance="Appearance.Accent" @onclick="OpenAddDialog">新增角色</FluentButton>
    <FluentButton Appearance="Appearance.Accent" @onclick="loadData">刷新数据</FluentButton>
</div>

<FluentDataGrid @ref="dataGrid" TGridItem="AppRoleDto" Items="@roles" Pagination="@pagination" AutoGenerateColumns="true" RowSize="@DataGridRowSize.Medium">
    <PropertyColumn Property="@(p => p.Id)" Sortable="true" />
    <PropertyColumn Property="@(p => p.RoleName)" Sortable="true" />
    <TemplateColumn Title="操作">
        <FluentButton Appearance="Appearance.Accent" @onclick="@(() => OpenEditDialog(context))">编辑</FluentButton>
        <FluentButton Appearance="Appearance.Accent" @onclick="@(() => ShowDeleteConfirmation(context))">删除</FluentButton>
        <FluentButton Appearance="Appearance.Accent" @onclick="@(() => OpenGrantPermissionDialog(context))">权限</FluentButton>
    </TemplateColumn>
</FluentDataGrid>
<FluentPaginator State="@pagination" />

@code {
    private FluentDataGrid<AppRoleDto> dataGrid;
    PaginationState pagination = new PaginationState() { ItemsPerPage = 5 };
    //Result<List<AppRoleDto>> roles = new Result<List<AppRoleDto>>(0, 0, new List<AppRoleDto>());  
    IQueryable<AppRoleDto> roles = new List<AppRoleDto>().AsQueryable();

    private async void loadData()
    {
        //var pageSize = pagination.ItemsPerPage == 0 ? 5 : pagination.ItemsPerPage;
        //var pageIndex = pagination.CurrentPageIndex == 0 ? 1 : pagination.CurrentPageIndex;
        //roles = roleService.GetAllPageing(pageIndex, pageSize);
        //roles = roleService.GetAll();
        //await pagination.SetTotalItemCountAsync(roles.count);        
        //this.StateHasChanged();
        await dataGrid.RefreshDataAsync();
    }

    protected override async Task OnInitializedAsync()
    {        
        roles = roleService.GetAll();
        //await pagination.SetTotalItemCountAsync(roles.count);
        //await pagination.SetCurrentPageIndexAsync(1);
        //roles = roleService.GetAllPageing(1, 5);
        //loadData();
        this.StateHasChanged();
    }

    private async Task OpenAddDialog()
    {
        var parameters = new DialogParameters<AppRoleDto>
        {
            Title = "新增角色",
            PrimaryAction = "确定",
            PrimaryActionEnabled = true,
            SecondaryAction = "取消",
            SecondaryActionEnabled = true,
            Width = "500px",
            TrapFocus = true,
            Modal = true,
            PreventScroll = true,
            Content = new AppRoleDto()
        };

        var dialog = await DialogService.ShowDialogAsync<RoleAdd>(parameters);
        DialogResult? result = await dialog.Result;
        if (!result.Cancelled)
        {
            loadData();
        }
    }

    private async Task OpenEditDialog(AppRoleDto role)
    {
        var parameters = new DialogParameters<AppRoleDto>
        {
            Title = $"编辑角色 - {role.RoleName}",
            PrimaryActionEnabled = false,
            SecondaryActionEnabled = false,
            Width = "500px",
            TrapFocus = true,
            Modal = true,
            PreventScroll = true,
            Content = role
        };

        var dialog = await DialogService.ShowDialogAsync<RoleEdit>(role, parameters);
        DialogResult? result = await dialog.Result;
        if (!result.Cancelled)
        {
            loadData();
        }
    }

    private async Task ShowDeleteConfirmation(AppRoleDto role)
    {
        var content = $"确定要删除角色 {role.RoleName} 吗？此操作无法撤销。";
        var confirmationDialog = await DialogService.ShowConfirmationAsync(
            message: content,
            primaryText: "确定",
            secondaryText: "取消",
            title: "确认删除?"
        );

        var result = await confirmationDialog.Result;
        if (!result.Cancelled)
        {
            await roleService.Delete(role.Id);
            loadData();
        }
    }

    private async Task OpenGrantPermissionDialog(AppRoleDto appRoleDto)
    {
        Console.WriteLine("Button clicked");
        Debug.WriteLine("Open dialog centered");        
        DialogParameters<AppRoleDto> parameters = new()
        {
            Title = $"权限分配",
            PrimaryAction = "确定",
            PrimaryActionEnabled = true,
            SecondaryAction = "取消",
            SecondaryActionEnabled = true,
            Width = "500px",
            TrapFocus = true,
            Modal = true,
            PreventScroll = true,
            Content = appRoleDto
        };        
        var dialog = await DialogService.ShowDialogAsync<GrantPermissionDialog>(appRoleDto, parameters);
        DialogResult? result = await dialog.Result;     
    }
}
